package com.example.dirviesystem.Admin.controller;

import com.example.dirviesystem.Admin.pojo.MessageData;
import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.CoachInfo;
import com.example.dirviesystem.entity.CoachMessage;
import com.example.dirviesystem.entity.CoachQualification;
import com.example.dirviesystem.repository.CoachInfoRepository;
import com.example.dirviesystem.repository.CoachMessageRe;
import com.example.dirviesystem.repository.CoachQualificationRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController

public class CoachController {

    @Autowired
    private CoachQualificationRe coachQualificationRe;
    @Autowired
    private CoachInfoRepository coachInfoRepository;
    @Autowired
    private CoachMessageRe coachMessageRe;


    @GetMapping("/coachQualifications")
    public List<CoachQualification> coachQualifications(){
        Boolean approved=false;
        return coachQualificationRe.findByApproved(approved);
    }
    @PostMapping("/createCoachQualificationMessage")
    public R createCoachQualificationMessage(@RequestBody MessageData coachMessageData){
        System.out.println(coachMessageData);
        CoachInfo coach = coachInfoRepository.findById(coachMessageData.getCoachId())
                .orElseThrow(() -> new RuntimeException("教练不存在"));

        CoachQualification qualification = coachQualificationRe.findById(coachMessageData.getQualificationId())
                .orElseThrow(() -> new RuntimeException("资质申请不存在"));
        CoachMessage message = new CoachMessage();
        message.setCoach(coach);
        message.setLook(false);
        message.setContent(coachMessageData.getMessageContent());
        message.setCreateTime(LocalDateTime.now());
        System.out.println(message);
        coachMessageRe.save(message);
        return R.success("消息发送成功");
    }
    @PostMapping("/approveCoachQualification/{qualificationId}")
    public R approveCoachQualification(@PathVariable Long qualificationId){
        CoachQualification qualification = coachQualificationRe.findById(qualificationId)
                .orElseThrow(() -> new RuntimeException("资质申请不存在"));

        // 标记审核通过
        qualification.setApproved(true);
        coachQualificationRe.save(qualification);

        // 同时更新教练信息中的资格状态
        CoachInfo coach = qualification.getCoach();
        coach.setQualified(true);
        coachInfoRepository.save(coach);

        return R.success("资质审核通过");

    }
    @PostMapping("/rejectCoachQualification/{qualificationId}")
    public R rejectCoachQualification(@PathVariable Long qualificationId){
        CoachQualification qualification = coachQualificationRe.findById(qualificationId)
                .orElseThrow(() -> new RuntimeException("资质申请不存在"));

        // 标记审核不通过
        qualification.setApproved(false);
        coachQualificationRe.save(qualification);
        // 同时更新教练信息中的资格状态
        CoachInfo coach = qualification.getCoach();
        coach.setQualified(false);
        coachInfoRepository.save(coach);

        return R.success("资质审核不通过");
    }

}
